<?php
namespace app\goods\controller\admin;

use app\admin\controller\Backend;

/**
 * 兑换订单管理
 * @internal
 */
class Order extends Backend
{

    protected $model;
   
    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('goods/goods_order', 'service');
    }

    /**
     * 订单确认
     */
    public function confrim() {
        if($this->request->isPost()) {
            $result = $this->model->confrim($this->request->post('order_no'));
            if(!$result) {
                return $this->error($this->model->getError());
            } else {
                return $this->success('success');
            }
        } else {
            return $this->error(lang('Network error'));
        }
    }


    public function delivery($order_no = '') {
        $row = $this->model->where(['order_no' => $order_no])->find();
        if($this->request->isPost()) {
            $delivery_code = $this->request->post('delivery_code');
            $delivery_sn = $this->request->post('delivery_sn');
            $result = $this->model->delivery($order_no, $delivery_code,  $delivery_sn);
            if(!$result) {
                return $this->error($this->model->getError());
            } else {
                return $this->success(lang('Operation completed'));
            }
        }
        return $this->view
            ->assign('deliverys', config('goods.deliverys'))
            ->assign('row', $row)
            ->fetch();
    }


    public function finish() {
        if($this->request->isPost()) {
            $result = $this->model->finish($this->request->post('order_no'));
            if(!$result) {
                return $this->error($this->model->getError());
            } else {
                return $this->success('success');
            }
        } else {
            return $this->error(lang('Network error'));
        }
    }

    public function cancel() {
        if($this->request->isPost()) {
            $result = $this->model->cancel($this->request->post('order_no'));
            if(!$result) {
                return $this->error($this->model->getError());
            } else {
                return $this->success('success');
            }
        } else {
            return $this->error(lang('Network error'));
        }
    }



}
